/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Patient;
import healthcare.coursework.service.AppointmentService;
import healthcare.coursework.service.FacilityService;
import healthcare.coursework.service.UserService;
import java.util.Date;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Timebox;
import org.zkoss.zul.Window;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author Administrator
 */
public class ArrangeLabTesting extends GenericAutowireComposer {

    private AppointmentService appointmentService = util.Utility.getUtility().getAppointmentService();
    private FacilityService facilityService=util.Utility.getUtility().getFacilityService();
    private UserService userService = util.Utility.getUtility().getUserService();
    private Textbox testName;
    private Combobox resourceName;
    private Datebox day;
    private Timebox time;
    private Label nameOfPatient;
    private Window self;
    private Patient patient;
    private Combobox laboratoryStaff;


    public void onLoadForm(Event e){
        setInfo();
    }

    private void setInfo() {
        patient=(Patient)arg.get("patient");
        nameOfPatient.setValue(patient.getFullName());
        resourceName.setModel(new ListModelList(facilityService.listOfFacilities()));
        laboratoryStaff.setModel(new ListModelList(userService.findLaboratories()));
        setConstraint();
    }

    private void setConstraint() {
        day.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateAppointTime(cmpnt, o);
            }
        });
    }

    private void validateAppointTime(Component comp, Object value) throws WrongValueException {
        Date d = (Date) value;
        if (d.before(new Date()) || d.equals(new Date())) {
            throw new WrongValueException(comp, "The day of appointment must be greater than current date!!!");
        }
    }

    public void onCancel(Event e) {
        self.onClose();
       // Executions.sendRedirect("PatientManagement.do");
    }

    public void onArrange(Event e) {
        try {

            Date appointmentTime=util.Utility.convertToDate(day.getValue(), time.getValue());
            appointmentService.makeRequestForLab((String)laboratoryStaff.getSelectedItem().getValue(), (Long) resourceName.getSelectedItem().getValue(),
                    patient.getPatientId(),testName.getValue(),appointmentTime);
            Messagebox.show("Appointment at time :"+
                    appointmentTime.toString()+ " for patient to make testing sample has been sent to administrative staff!", "Information", Messagebox.OK, Messagebox.INFORMATION);
            self.onClose();
            Executions.sendRedirect("PatientManagement.do");
        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage());
        }
    }
   
}
